﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace WorkRecord
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private List<Label> rqList = new List<Label>();
        private List<Label> jbList = new List<Label>();
        private List<Label> qjList = new List<Label>();
        private List<Panel> zgList = new List<Panel>();
        private XmlDocument doc = new XmlDocument();
        private MyDate myDate = new MyDate();

        private void Form1_Load(object sender, EventArgs e)
        {
            doc.Load(@"./WorkRecord.xml");
            this.FindControls(this.Controls);
            this.dateTimePicker.Value = DateTime.Now;
        }

        private void FindControls(Control.ControlCollection Coll)
        {
            foreach (var con in Coll)
            {
                if (con is Panel)
                {
                    Panel pan = con as Panel;
                    if (pan.Name.StartsWith("p_zg_"))
                    {
                        zgList.Add(pan);
                    }
                    this.FindControls(pan.Controls);
                }
                else if (con is Label)
                {
                    Label lab = con as Label;
                    if (lab.Name.StartsWith("l_rq_"))
                    {
                        lab.DoubleClick += new System.EventHandler(this.Input_DoubleClick);
                        this.rqList.Add(lab);
                    }
                    if (lab.Name.StartsWith("l_jb_"))
                    {
                        this.jbList.Add(lab);
                    }
                    if (lab.Name.StartsWith("l_qj_"))
                    {
                        this.qjList.Add(lab);
                    }
                }
            }
        }

        private void DrawCalendar()
        {
            int TotalDays = DateTime.DaysInMonth(myDate.CaculYear, myDate.CaculMonth);
            int dayOfWeek = Convert.ToInt32(new DateTime(myDate.CaculYear, myDate.CaculMonth, 26).DayOfWeek);
            dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek;
            int start = 26;
            decimal[,] dayData = new decimal[TotalDays, 4];
            for (int i = 0; i < TotalDays; i++)
            {
                if (start > TotalDays)
                {
                    start = 1;
                }
                dayData[i, 0] = start++;
            }
            this.GetDayDetailed(dayData);
            this.GetSummary(dayData);
            start = 0;
            for (int i = 1; i <= 37; i++)
            {
                if (i < dayOfWeek || i >= dayOfWeek + TotalDays)
                {
                    zgList[37 - i].Visible = false;
                }
                else
                {
                    zgList[37 - i].Visible = true;
                    rqList[37 - i].Text = dayData[start, 0].ToString();
                    jbList[37 - i].Text = dayData[start, 1] == 0 ? "--" : dayData[start, 1].ToString();
                    qjList[37 - i].Text = dayData[start, 2] == 0 ? "--" : dayData[start, 2].ToString();
                    start++;
                }
            }
        }

        private void GetDayDetailed(decimal[,] dayData)
        {
            XmlNode WorkRecord = doc.SelectSingleNode("WorkRecord");
            foreach (XmlNode Year in WorkRecord.ChildNodes)
            {
                if (Convert.ToInt32(Year.Attributes["year"].Value) == myDate.ConverYear)
                {
                    foreach (XmlNode Month in Year.ChildNodes)
                    {
                        if (Convert.ToInt32(Month.Attributes["month"].Value) == myDate.ConverMonth)
                        {
                            foreach (XmlNode Day in Month.ChildNodes)
                            {
                                for (int i = 0; i < dayData.Length / 4; i++)
                                {
                                    if (dayData[i, 0] == Convert.ToInt32(Day.Attributes["day"].Value))
                                    {
                                        dayData[i, 1] = Convert.ToDecimal(Day.Attributes["jbhour"].Value);
                                        dayData[i, 2] = Convert.ToDecimal(Day.Attributes["qjhour"].Value);
                                        dayData[i, 3] = Convert.ToInt32(Day.Attributes["type"].Value);
                                    }
                                }
                            }
                            break;
                        }
                    }
                    break;
                }
            }
        }

        private void GetSummary(decimal[,] dayData)
        {
            decimal dHz = 0;
            decimal dGzr = 0;
            decimal dXxr = 0;
            decimal dJjr = 0;
            decimal dQj = 0;
            for (int i = 0; i < dayData.Length / 4; i++)
            {
                switch (dayData[i, 3])
                {
                    case 1:
                        dGzr += dayData[i, 1];
                        break;
                    case 2:
                        dXxr += dayData[i, 1];
                        break;
                    case 3:
                        dJjr += dayData[i, 1];
                        break;
                    default:
                        break;
                }
                dQj += dayData[i, 2];
            }
            dHz = dGzr + dXxr + dJjr - dQj;
            this.t_hz.Text = dHz.ToString();
            this.t_gzr.Text = dGzr.ToString();
            this.t_xxr.Text = dXxr.ToString();
            this.t_jjr.Text = dJjr.ToString();
            this.t_qj.Text = dQj.ToString();

        }

        private void dateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            myDate.DateTime = dateTimePicker.Value;
            DrawCalendar();
        }

        private void Input_DoubleClick(object sender, EventArgs e)
        {
            DateTime dateTime = myDate.GetRealDate(myDate.ConverYear, myDate.ConverMonth, Convert.ToInt32((sender as Label).Text));
            FormInput formInput = new FormInput(dateTime);
            formInput.StartPosition = FormStartPosition.CenterParent;
            formInput.ShowDialog();
            doc.Load(@"./WorkRecord.xml");
            DrawCalendar();
        }
    }
}